import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like } from 'typeorm';
import { Roles } from '../entity/roles.entity'
import { PageInit, PageNation } from 'src/interface/page.interface';
type T = any;
@Injectable()
export class RolesService {
    constructor(
        @InjectRepository(Roles)
        private rolesRepository: Repository<Roles>,
    ) { }
    //创建角色
    async create(role: Roles): Promise<Roles> {
        return await this.rolesRepository.save(role);
    }

    //更新角色
    async update(role: Roles): Promise<any> {
        try {
            const res = await this.rolesRepository.save(role)
            return res
        } catch (error) {
            throw new (error)
        }

    }
    async findAll(query: PageInit): Promise<PageNation> {
        //  console.log(query)
        const take = query.take || 10
        const skip = query.skip || 0
        const keyword = query.keyword || ''

        let res = await this.rolesRepository.findAndCount({
            where: { role_name: Like('%' + keyword + '%') }, order: { role_name: "DESC" },
            take: take,
            skip: skip
        })

        const [result, total] = res;
        console.log('输出res:', result.length)
        return {
            list: result,
            count: total
        }
    }
}
